package com.sm.service.Impl;

import com.sm.dao.UserDao;
import com.sm.dao.UserWelfareDao;
import com.sm.dao.WagesDao;
import com.sm.dao.WelfareDao;
import com.sm.entity.UserInfo;
import com.sm.entity.Welfare;
import com.sm.service.WelfareService;
import com.sm.utils.AttendanceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class WelfareServiceImpl implements WelfareService {

    @Autowired
    WelfareDao welfareDao;
    @Autowired
    UserWelfareDao userWelfareDao;
    @Autowired
    WagesDao wagesDao;
    @Autowired
    UserDao userDao;

    public void addWelfare(Welfare welfare) {
        welfareDao.addWelfare(welfare);
    }

    public void addWelfareForUser(Integer uid, Integer wid) {
        Integer month = AttendanceUtil.getMonth();
        System.out.println(uid);
        userWelfareDao.addUserWelfare(uid,wid,month);
        Double amount = welfareDao.findAmountByWid(wid);
        wagesDao.updateWelfare(uid,amount,month );
    }

    public List<Welfare> findAllWelfare() {
        return welfareDao.findAllWelfare();
    }

    public List<Welfare> findWelfareByUid(Integer uid) {
        return welfareDao.findWelfareByUid(uid);
    }

    public List<UserInfo> findUser(String uid, String name)throws Exception {
        List<UserInfo> users = new ArrayList<UserInfo>();
        if(uid != null){
            users.add(userDao.findByUid(Integer.valueOf(uid)));
        }else {
            List<Integer> uids = userDao.findUidByUsernameOrName(null, name);
            if(uids==null||uids.size()==0)return users;
            for(Integer id:uids){
                users.add(userDao.findByUid(id));
            }
        }
        return users;
    }
}
